package com.bdqn.controller;

import com.bdqn.base.R;
import com.bdqn.pojo.User;
import com.bdqn.service.UserService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * UserController
 *
 * @author LILIBO
 * @since 2022-02-20
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @Resource
    private UserService userService;

    @PostMapping("/create")
    public R createUser(User user) {
        boolean save = userService.save(user);
        if (!save) {
            return R.failure("操作失败");
        }
        return R.success(user);
    }

    @GetMapping("/list")
    public R getList() {
        List<User> list = userService.getList(null);
        return R.success(list);
    }

    @GetMapping("/{id}")
    public R getUser(@PathVariable("id") Long usrId) {
        if (usrId == null) {
            return R.failure("用户ID不能为空");
        }
        User user = userService.getById(usrId);
        if (user == null) {
            return R.failure("用户不存在");
        }
        return R.success(user);
    }
}
